package cn.jiyun.controller;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService usersService;

    //页面跳转
    @RequestMapping("{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    //添加用户
    @RequestMapping("/addUser")
    public String addUser(User user) {
        usersService.addUser(user);
        return "redirect:/user/listUser";
    }

    //展示用户
    @RequestMapping("/listUser")
    public String listUser(Model model) {
        List<User> list = usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }


    //删除信息
    @RequestMapping("/delete")
    public String delete(Integer id) {
        usersService.delete(id);
        return "redirect:/user/listUser";
    }

    //回显
    @RequestMapping("/findById")
    public String findById(Integer id,Model model) {
        User user = usersService.findById(id);
        model.addAttribute("listUser",user);
        return "/update";
    }

    //修改用户
    @RequestMapping("/updateUser")
    public String updateUser(User user,Model model) {
       usersService.updateUser(user);
        return "redirect:/user/listUser";
    }

}
